treaty <- read.csv("CYEstimateLatentHRtreaty04.csv")

FULL <- TRUE
if(FULL==TRUE)treaty$TREATY <- apply(treaty[,4:(ncol(treaty)-2)],1,sum,na.rm=T)
if(FULL==TRUE)treaty <- subset(treaty, select=c(ccode, year, TREATY, latentmean, latentsd))
if(FULL==FALSE)treaty <- subset(treaty, select=c(ccode, year, cat_rn, cescr_rn, cerd_rn, ccpr_rn, cedaw_rn, crc_rn, latentmean, latentsd))
if(FULL==TRUE)names(treaty) <- c("COW", "YEAR","TREATY", "latentmean", "latentsd")
if(FULL==FALSE)names(treaty) <- c("COW", "YEAR", "cat", "cescr", "cerd", "ccpr", "cedaw", "crc", "latentmean", "latentsd")

# this is not correct but necessary for some replications
if(FULL==FALSE){
  WRONG <- TRUE
  if(WRONG==TRUE)treaty$cat[is.na(treaty$cat)] <- 0
  if(WRONG==TRUE)treaty$cescr[is.na(treaty$cescr)] <- 0
  if(WRONG==TRUE)treaty$cerd[is.na(treaty$cerd)] <- 0
  if(WRONG==TRUE)treaty$ccpr[is.na(treaty$ccpr)] <- 0
  if(WRONG==TRUE)treaty$cedaw[is.na(treaty$cedaw)] <- 0
  if(WRONG==TRUE)treaty$crc[is.na(treaty$crc)] <- 0
}

if(FULL==FALSE)treaty$TREATY <- treaty$cat + treaty$cescr + treaty$cerd + treaty$ccpr + treaty$cedaw + treaty$crc

data <- subset(data, year>=1976)

test <- NA
for(i in 1:1000){
  test[i] <- cor(treaty$TREATY, rnorm(rep(1,nrow(treaty)), mean=treaty$latentmean, sd=treaty$latentsd))
}
quantile(test, c(0.025, 0.5, 0.975))

COLOR <- colorRampPalette(blues9[-(1:3)])
COLOR <- colorRampPalette(c(grey(.8),grey(.6),grey(.2)))

par(mar=c(6,6,1,1))
plot(treaty$latentmean, treaty$TREATY, xlim=c(-6,6), pch=3, cex=.7, col=densCols(treaty$latentmean, treaty$additive, colramp=COLOR), xaxt="n", yaxt="n", xlab="", ylab="")
if(FULL==TRUE)axis(side=2, at=c(0,5,10,15,20,25,30), las=2, font=1)
if(FULL==FALSE)axis(side=2, at=c(0:6), las=2, font=1)
axis(side=1, at=-6:6, labels=-6:6, las=1, font=1)
if(FULL==TRUE)mtext(side=2, "Additive Scale (ALL Treaties)", cex=1.7, line=3.5)
if(FULL==FALSE)mtext(side=2, "Additive Scale \n(CAT, CCPR, CESCR, CERD, CEDAW, CRC)", cex=1.7, line=2.5)
mtext(side=1, "Latent Treaty Variable", cex=1.7, line=4)
